/**
 * @param {number[]} nums
 * @return {number}
 */
export var specialArray = function (nums) {
  nums.sort((a, b) => a - b)
  let [left, right] = [0, nums.length - 1]
  let ansIdx = -1
  while (left <= right) {
    const mid = (left + right) >> 1
    const cnt = nums.length - mid
    if (nums[mid] >= cnt) {
      ansIdx = mid
      right = mid - 1
    } else left = mid + 1
  }
  if (ansIdx === -1) return -1
  return nums[ansIdx - 1] === nums.length - ansIdx ? -1 : nums.length - ansIdx
}
